using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PlugNT.Safe;
using PlugNT.Database;
using PlugNT.Database.Common.Simple;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Model;

namespace PlugNT.Cms.DAL
{
    public class GroupPages
    {


        private static string currTableName = WebConfig.TablePrefix + "group_pages";








        public GroupPagesInfo GetGroupPagesInfo(string codeNo)
        {
            GroupPagesInfo model = null;
            IDataReader dr = GetReader(1, "*", " and [code_no]='" + StringHelper.SqlFilter(codeNo) + "'");
            if (dr != null)
            {
                if (dr.Read())
                {
                    model = new GroupPagesInfo();
                    model.id = int.Parse(dr["id"].ToString());
                    model.code_no = dr["code_no"].ToString();
                    model.page_name = dr["page_name"].ToString();
                    model.page_path = dr["page_path"].ToString();
                    model.group_name = dr["group_name"].ToString();
                }
                dr.Close();
            }
            return model;
        }

        public bool Exist(string codeNo)
        {
            string sql = "Select top 1 id From " + currTableName + " where [code_no]='" + StringHelper.SqlFilter(codeNo) + "'";
            return  DbHelper.Exists(sql);
        }

        public string GetPageName(string codeNo)
        {
            string sql = "Select top 1 page_name From " + currTableName + " where [code_no]='" + StringHelper.SqlFilter(codeNo) + "'";
            object obj = DbHelper.GetSingle(sql);
            return (obj == null) ? "" : obj.ToString();
        }











        public bool Add(GroupPagesInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into " + currTableName);
            strSql.Append(" ([code_no],[page_name],[page_path],[group_name]) values (");
            strSql.Append("'" + StringHelper.SqlFilter(model.code_no) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.page_name) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.page_path) + "',");
            strSql.Append("'" + StringHelper.SqlFilter(model.group_name) + "'");            
            strSql.Append(")");

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }






        public bool Update(GroupPagesInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update " + currTableName);
            strSql.Append(" set [page_name]='" + StringHelper.SqlFilter(model.page_name) + "',");
            strSql.Append("[page_path]='" + StringHelper.SqlFilter(model.page_path) + "',");
            strSql.Append("[group_name]='" + StringHelper.SqlFilter(model.group_name) + "',");
            strSql.Append("[code_no]='" + StringHelper.SqlFilter(model.code_no) + "'");
            strSql.Append(" where [id]=" + model.id.ToString());

            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }


        public bool Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where [id]= "+id.ToString());
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }

        public bool Delete(string codeNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from " + currTableName);
            strSql.Append(" where [code_no]='" + StringHelper.SqlFilter(codeNo) + "'");
            return (DbHelper.ExecuteSql(strSql.ToString()) > 0) ? true : false;
        }










        private IDataReader GetReader(int topCount, string colNames, string andWhere)
        {
            string sql = "select " + (topCount == -1 ? "" : "top " + topCount) + " " + colNames + " from " + currTableName + " where 1=1 " + andWhere;
            return DbHelper.ExecuteReader(sql);
        }







        public DataTable GetList(string andWhere)
        {
            string sql = "select * from " + currTableName + " where 1=1 " + andWhere + " order by group_name";
            return DbHelper.GetDataTable(sql);
        }

        public DataTable GetList(int topCount, int currPageCount, string andWhere)
        {
            string sql = Sql.GetRePagingString(topCount, currPageCount, currTableName, "id", andWhere);
            return DbHelper.GetDataTable(sql);
        }


        public int GetCount(string andWhere)
        {
            string sql = "select count(1) from [" + currTableName + "] where 1=1 " + andWhere;
            return (int)DbHelper.GetSingle(sql);
        }

    }
}

